#include <iostream>
#include <map>
#include <set>
using namespace std;

typedef set <int> si;
typedef si::iterator itsi;
typedef map <int, si> misi;


misi reachable;


int main ()
{
	int t, m, c, k, g, m_left;
	itsi it;

	cin >> t;
	while (t--)
	{
		reachable.clear();
		cin >> m >> c;

		{
			si &s = reachable[0];
			cin >> k;
			for (int j = 0; j < k; ++j)
			{
				cin >> g;
				m_left = m - g;
				if (m_left >= 0)
				{
					s.insert (m_left);
				}
			}
		}

		for (int i = 1; i < c; ++i)
		{
			si	&s = reachable[i],
				&s_old = reachable[i-1];
			cin >> k;
			for (int j = 0; j < k; ++j)
			{
				cin >> g;
				for (it = s_old.begin(); it != s_old.end(); ++it)
				{
					m_left = *it - g;
					if (m_left >= 0)
					{
						s.insert (m_left);
					}
				}
			}
		}

		{
			si &s = reachable[c-1];
			it = s.begin();
			if (it == s.end())
				cout << "no solution\n";
			else
				cout << (m - *it) << '\n';
		}

	}
}